package com.woorigil.social.controller.api.socialshopping;

import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.woorigil.social.MobileAppType;
import com.woorigil.social.controller.BaseController;
import com.woorigil.social.domain.ApplicationService;
import com.woorigil.social.domain.ProviderCode;
import com.woorigil.social.domain.app.MobileApp;
import com.woorigil.social.domain.deal.DealFindService;
import com.woorigil.social.domain.deal.DealList;
import com.woorigil.social.domain.deal.RecentClick;
import com.woorigil.social.domain.deal.RecentClickDao;
import com.woorigil.social.domain.deal.finder.CategoryDealFindCaller;
import com.woorigil.social.domain.deal.finder.EndDealFindCaller;
import com.woorigil.social.domain.deal.finder.FreeDealFindCaller;
import com.woorigil.social.domain.deal.finder.NewDealFindCaller;
import com.woorigil.social.domain.deal.repository.DealListCondition;
import com.woorigil.social.domain.qna.QnaFindService;
import com.woorigil.social.domain.qna.QnaList;
import com.woorigil.social.domain.query.QueryService;

@Controller
@RequestMapping("/api/socialshopping/v20")
public class Version2_0_ApiController extends BaseController {
	@Autowired QueryService queryService;
	@Autowired DealFindService dealFindService;
	@Autowired QnaFindService qnaFindService;
	@Autowired ApplicationService applicationService;
	@Autowired RecentClickDao recentClickDao;
	
	
	@RequestMapping("/infomation/all")
	public ModelAndView informations(String sid) {
		MobileApp mobileApp = applicationService.getLastReleaseAppVersion(MobileAppType.SOCIAL_SHOPPING);
		
		ModelAndView mav = new ModelAndView("");
		if (mobileApp != null) {
			mav.addObject("appVersion", mobileApp.getVersion());
			mav.addObject("adPlatformType", mobileApp.getAdPlatformType());
			mav.addObject("useAdPlatform", mobileApp.isUseAdPlatform());
		}
		mav.addObject("dealListLimitCount", DealListCondition.defaultLimit);
		mav.addObject("dealTotalCount", applicationService.getTotalItemCount());
		mav.addObject("lastUpdatedTime", applicationService.getLastUpdatedAt().getTime());
		mav.addObject("eventName", "");
		mav.addObject("categories", applicationService.getCategories());
		mav.addObject("providers", applicationService.getProviders());
		return mav;
	}
	
//	@RequestMapping("/deals/all")
//	public ModelAndView allDeals(
//			String sid, 
//			int mainCategoryId, 
//			int subCategoryId, 
//			int offset,
//			@RequestParam(defaultValue="") String nowLat, 
//			@RequestParam(defaultValue="") String nowLon) {
//
//		DealList categoryDealList = dealFindService.findCategoryDealList(sid, mainCategoryId, subCategoryId, offset, nowLat, nowLon);
//		DealList newDealList = dealFindService.findNewDealList(sid, mainCategoryId, subCategoryId, offset);
//		DealList endDealList = dealFindService.findEndDealList(sid, mainCategoryId, subCategoryId, offset);
//		DealList freeDealList = dealFindService.findFreeDealList(sid, mainCategoryId, subCategoryId, offset);
//		
//		ModelAndView mav = new ModelAndView("");
//		mav.addObject("categoryDeals", categoryDealList.getDeals());
//		mav.addObject("newDeals", newDealList.getDeals());
//		mav.addObject("endDeals", endDealList.getDeals());
//		mav.addObject("freeDeals", freeDealList.getDeals());
//		return mav;
//	}		
	
	@RequestMapping("/deals/all")
	public ModelAndView allDeals(
			String sid, 
			int mainCategoryId, 
			int subCategoryId, 
			int offset,
			@RequestParam(defaultValue="") String nowLat, 
			@RequestParam(defaultValue="") String nowLon) throws Exception {
		
		ExecutorService executor = Executors.newFixedThreadPool(4);
		Future<DealList> categoryDealFuture = executor.submit(new CategoryDealFindCaller(dealFindService, sid, mainCategoryId, subCategoryId, offset, nowLat, nowLon));
		Future<DealList> newDealFuture = executor.submit(new NewDealFindCaller(dealFindService, sid, mainCategoryId, subCategoryId, offset, nowLat, nowLon));
		Future<DealList> endDealFuture = executor.submit(new EndDealFindCaller(dealFindService, sid, mainCategoryId, subCategoryId, offset, nowLat, nowLon));
		Future<DealList> freeDealFuture = executor.submit(new FreeDealFindCaller(dealFindService, sid, mainCategoryId, subCategoryId, offset, nowLat, nowLon));
		
		DealList categoryDealList = categoryDealFuture.get();
		DealList newDealList = newDealFuture.get();
		DealList endDealList = endDealFuture.get();
		DealList freeDealList = freeDealFuture.get();
		
		ModelAndView mav = new ModelAndView("");
		mav.addObject("categoryDeals", categoryDealList.getDeals());
		mav.addObject("newDeals", newDealList.getDeals());
		mav.addObject("endDeals", endDealList.getDeals());
		mav.addObject("freeDeals", freeDealList.getDeals());
		return mav;
	}		
	
	@RequestMapping("/deals/category")
	public ModelAndView categoryDeals(
			String sid, 
			int mainCategoryId, 
			int subCategoryId, 
			int offset,
			@RequestParam(defaultValue="") String nowLat, 
			@RequestParam(defaultValue="") String nowLon) {
		DealList categoryDealList = dealFindService.findCategoryDealList(mainCategoryId, subCategoryId, offset, nowLat, nowLon);
		ModelAndView mav = new ModelAndView("");
		mav.addObject("categoryDeals", categoryDealList.getDeals());
		return mav;
	}
	
	@RequestMapping("/deals/distance")
	public ModelAndView distance(
			String sid, 
			int mainCategoryId, 
			int subCategoryId, 
			int offset,
			@RequestParam(defaultValue="") String nowLat, 
			@RequestParam(defaultValue="") String nowLon) {
		
		DealList distanceDealList = dealFindService.findLocationDealList(mainCategoryId, subCategoryId, offset, nowLat, nowLon);
		ModelAndView mav = new ModelAndView("");
		mav.addObject("distanceDeals", distanceDealList.getDeals());
		return mav;
	}		
	
	@RequestMapping("/deals/new")
	public ModelAndView newDeals(String sid, int mainCategoryId, int subCategoryId, int offset) {
		DealList newDealList = dealFindService.findNewDealList(mainCategoryId, subCategoryId, offset);
		ModelAndView mav = new ModelAndView("");
		mav.addObject("newDeals", newDealList.getDeals());
		return mav;
	}
	
	@RequestMapping("/deals/end")
	public ModelAndView endDeals(String sid, int mainCategoryId, int subCategoryId, int offset) {
		DealList endDealList = dealFindService.findEndDealList(mainCategoryId, subCategoryId, offset);
		ModelAndView mav = new ModelAndView("");
		mav.addObject("endDeals", endDealList.getDeals());
		return mav;
	}
	
	@RequestMapping("/deals/free")
	public ModelAndView freeDeals(String sid, int mainCategoryId, int subCategoryId, int offset) {
		DealList freeDealList = dealFindService.findFreeDealList(mainCategoryId, subCategoryId, offset);
		ModelAndView mav = new ModelAndView("");
		mav.addObject("freeDeals", freeDealList.getDeals());
		return mav;
	}		
	
	@RequestMapping("/deals/event")
	public ModelAndView eventDeals(String sid, int offset) {
		DealList eventDealList = dealFindService.findEventDealList(sid, offset);
		ModelAndView mav = new ModelAndView("");
		mav.addObject("eventDeals", eventDealList.getDeals());
		return mav;
	}		
	
	@RequestMapping("/recent/view")
	public ModelAndView recentlyViewedItems(String sid, int offset) {
		ModelAndView mav = new ModelAndView("");
		mav.addObject("recentViewDeals", dealFindService.findViewedDeals(sid, offset));
		return mav;
	}
	
	@RequestMapping("/recent/click")
	public ModelAndView getRecentClick(String sid, int offset) {
		ModelAndView mav = new ModelAndView("");
		mav.addObject("recentClickDeals", dealFindService.findClickedDeals(sid, offset));
		return mav;
	}
	
	@RequestMapping("/recent/click/add")
	public ModelAndView recentClickAdd(String sid, int dealId) {
		if (StringUtils.isNotBlank(sid)) {
			RecentClick recentClick = recentClickDao.selectByDeal(sid, dealId);
			if (recentClick == null) {
				recentClickDao.insert(new RecentClick(sid, dealId, new Date()));
			} else {
				recentClick.setInsertDate(new Date());
				recentClickDao.update(recentClick);
			}
		}
		
		return new ModelAndView("");
	}	
	
	@RequestMapping("/query/all")
	public ModelAndView recentQueries(String sid) {
		ModelAndView mav = new ModelAndView("");
		mav.addObject("queryBox", queryService.findAllBySid(sid));
		return mav;
	}
	
	@RequestMapping("/query/delete")
	public ModelAndView recentQueries(String sid, String query) {
		ModelAndView mav = new ModelAndView("");
		mav.addObject("count", queryService.deleteRecentQueryBySidAndQuery(sid, query));
		return mav;
	}

	@RequestMapping("/search")
	public ModelAndView search(String sid, String query, int offset) {
		ModelAndView mav = new ModelAndView("");
		DealList dealList = dealFindService.search(sid, query, offset);
		mav.addObject("deals", dealList.getDeals());
		return mav;
	}
	
	@RequestMapping("/deal/qna")
	public ModelAndView qna(String sid, int dealId, String providerCode, String metaId) {
		ModelAndView mav = new ModelAndView("");
		QnaList qnaList = qnaFindService.findQna(sid, dealId, ProviderCode.valueOf(providerCode), metaId);
		mav.addObject("qnaList", qnaList);
		return mav;
	}
}
